package beautiful.butterfly.schedule.zookeeper;

import beautiful.butterfly.schedule.Constants;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ZooKeepers {
    /**
     * 创建节点
     */
    public static void createPath(ZooKeeper zooKeeper, String path, List<ACL> aclList, CreateMode createMode) throws Exception {
        String[] strings = path.split(Constants.slash);
        String temporaryPath = "";
        for (String string : strings) {
            if (string.equals("") == false) {
                temporaryPath = temporaryPath + Constants.slash + string;
                if (zooKeeper.exists(temporaryPath, false) == null) {
                    zooKeeper.create(temporaryPath, null, aclList, createMode);
                }
            }
        }
    }


    /**
     * 递归删除
     */
    public static void directoryStructureDelete(ZooKeeper zooKeeper, String path) throws Exception {
        String[] paths = directoryStructureGetPathList(zooKeeper, path);
        for (int i = paths.length - 1; i >= 0; i--) {//从底层删除到最上面
            zooKeeper.delete(paths[i], -1);
        }
    }

    /**
     * 递归的获取子节点信息,最后以目录形式展现
     */
    public static String[] directoryStructureGetPathList(ZooKeeper zooKeeper, String path) throws Exception {
        if (zooKeeper.exists(path, false) == null) {
            return new String[0];
        }
        List<String> pathList = new ArrayList<String>();
        pathList.add(path);
        int index = 0;
        while (index < pathList.size()) {//这样避免重复遍历
            String temporaryPath = pathList.get(index);
            List<String> childrenPathList = zooKeeper.getChildren(temporaryPath, false);
            //add /
            if (temporaryPath.equalsIgnoreCase(Constants.slash) == false) {
                temporaryPath = temporaryPath + Constants.slash;
            }
            Collections.sort(childrenPathList);
            for (int i = childrenPathList.size() - 1; i >= 0; i--) {
                // 用于在列表的指定位置插入指定元素，并将当前处于该位置的元素及其后续元素的索引加1。void add(int index, E element)
                pathList.add(index + 1, temporaryPath + childrenPathList.get(i));
            }
            index++;
        }
        return pathList.toArray(new String[0]);
    }

    /**
     * 以目录结构的形式进行显示
     */
    public static void showInDirectoryStructureWay(ZooKeeper zooKeeper, String path, Writer writer, String lineSplit) throws Exception {
        String[] paths = directoryStructureGetPathList(zooKeeper, path);
        Stat stat = new Stat();
        for (String temporaryPath : paths) {
            byte[] bytes = zooKeeper.getData(temporaryPath, false, stat);
            if (bytes == null) {
                writer.write(temporaryPath + lineSplit);
            } else {
                writer.write(temporaryPath + "[v." + stat.getVersion() + "]" + "[" + new String(bytes) + "]" + lineSplit);
            }
        }
    }
}
